package com.hp.controller;

import com.hp.entity.request.AlarmContentRequest;
import com.hp.entity.request.ContentRequest;
import com.hp.entity.response.MessageResponse;
import com.hp.service.SendMessageService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @author hp
 */
@RestController
@RequestMapping("/lark")
public class SendMessageController {

    @Resource
    private SendMessageService sendMessageService;


    @GetMapping("/send")
    public MessageResponse send() {
        AlarmContentRequest request = setAlarmContentRequest(1, 1, 1, new Exception("test"));
        return sendMessageService.sendMessage(request);
    }

    @GetMapping("/sendAsync")
    public Boolean sendAsync() {
        AlarmContentRequest request = setAlarmContentRequest(2, 2, 2, new Exception("test"));
        sendMessageService.sendMessageAsync(request);
        return true;
    }


    /**
     * 拼装发送lark请求对象
     */
    private AlarmContentRequest setAlarmContentRequest(Integer studentId, int size, int count, Exception e) {
        List<ContentRequest> contentList = Arrays.asList(
                new ContentRequest().setText("** 总共【" + size + "】条数据"),
                new ContentRequest().setText("** 更新了【" + count + "】条数据"),
                new ContentRequest().setText("** 错误studentId：【" + studentId + "】"),
                new ContentRequest().setText("** 错误信息：【" + e.getMessage() + "】")
        );
        AlarmContentRequest request = new AlarmContentRequest();
        request.setTitle("更新失败").setContentList(contentList);
        return request;
    }
}
